package com.power.flowable.api;

import com.plf.common.domain.ResponseDTO;
import com.power.flowable.model.pageinfo.Aprv_homePageInfo;
import com.power.flowable.model.BackTaskVo;
import com.power.flowable.model.ProcessNextExecutor;
import com.power.flowable.model.pageinfo.BusinessPageInfo;
import com.power.flowable.model.pageinfo.ExecutionPageInfo;
import com.power.flowable.model.pageinfo.TaskPageInfo;
import feign.Response;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 个人流程业务相关接口
 *
 * @author wangning
 *
 */
@FeignClient(value = "sfflow-svr", contextId = "sfflow-svr", path = "/sfflow-svr")
@RestController
public interface IPersonalFeign {
	/**
	 * 根据userid查询需要处理的业务
	 *
	 * @param userid
	 * @param pageinfo
	 * @return
	 */
	@PostMapping("flow/taskList")
	ResponseDTO taskList(@RequestParam String userid, @Valid @RequestBody BusinessPageInfo pageinfo);

	/**
	 * 查询只要父流程列表
	 *
	 * @param userid
	 * @param pageinfo
	 * @return
	 */
	@PostMapping("flow/collectBusinessList")
	ResponseDTO collectBusinessList(@RequestParam String userid, @RequestBody BusinessPageInfo pageinfo);

	/**
	 * 查询子流程的详细列表
	 *
	 * @param userid
	 * @param pageinfo
	 * @return
	 */
	@PostMapping("flow/detailsBusinessList")
	ResponseDTO detailsBusinessList(@RequestParam String userid, @RequestBody BusinessPageInfo pageinfo);

	/**
	 * 查询子流程的历史详细列表
	 *
	 * @param userid
	 * @param pageinfo
	 * @return
	 */
	@PostMapping("flow/detailsBusinessHistoryList")
	ResponseDTO detailsBusinessHistoryList(@RequestParam String userid, @RequestBody BusinessPageInfo pageinfo);

	/**
	 * 根据userid查询挂起的业务
	 * @param userid
	 * @param pageinfo
	 * @return
	 */
	@PostMapping("flow/suspendBusinessList")
	ResponseDTO suspendBusinessList(@RequestParam  String userid, @RequestBody TaskPageInfo pageinfo);

	/**
	 * 根据userid查询需要处理的业务
	 * @param pageInfo
	 * @return
	 */
	@PostMapping("flow/userTask")
	ResponseDTO userTask(@RequestBody Aprv_homePageInfo pageInfo);

	/**
	 * 查询执行者为“组”类型的业务列表
	 *
	 * @param groupid 组id
	 * @return
	 */
	@PostMapping("flow/groupTaskList")
	ResponseDTO groupTaskList(@RequestParam String groupid);

	/**
	 * 根据taskid查看业务信息
	 *
	 * @param taskid (对应act_ru_task表_id字段)
	 * @return
	 */
	@PostMapping("flow/taskView")
	ResponseDTO taskView(@RequestParam String taskid);

	/**
	 * 根据userid查询由我发起的流程列表
	 *
	 * @param userid
	 * @return
	 */
	@PostMapping("flow/startedList")
	ResponseDTO startedList(@RequestParam String userid);

	/**
	 * 删除流程实例
	 *
	 * @param processinstanceid (流程实例id, 对应act_ru_task表的PROC_INST_ID_字段)
	 * @param deleteReason      删除原因
	 * @return
	 */
	@PostMapping("flow/processInstanceRemove")
	ResponseDTO processInstanceRemove(@RequestParam String processinstanceid, @RequestParam(required = false) String deleteReason);

	/**
	 * 业务分配给了某一组人员时，需要该组人员进行抢占。抢到了就将该业务给谁处理，其他人不能处理。
	 *
	 * @param taskid 任务id
	 * @param userid
	 * @return
	 */
	@PostMapping(value = "flow/claimTask")
	ResponseDTO claimTask(@RequestParam String taskid, @RequestParam String userid);

	/**
	 * 业务分配给了某一组人员时，需要该组人员进行抢占。抢到了就将该业务给谁处理，其他人不能处理。
	 *
	 * @param processinstanceid 实例id
	 * @param userid
	 * @return
	 */
	@PostMapping(value = "flow/claimTaskByInstid")
	ResponseDTO claimTaskByInstid(@RequestParam String processinstanceid, @RequestParam String userid);


	/**
	 * 批量分派任务给某一组人员时，需要该组人员进行抢占。抢到了就将该业务给谁处理，其他人不能处理。
	 * @param taskids
	 * @param userid
	 * @return
	 */
	@PostMapping(value = "flow/claimTaskBatch")
	ResponseDTO claimTaskBatch(@RequestBody List<String> taskids, @RequestParam String userid);

	/**
	 * 获取流程定义开始表单
	 *
	 * @param processdefinitionid 流程定义id(对应act_ru_task表的PROC_DEF_ID_字段)
	 * @return
	 */
	@PostMapping(value = "flow/loadStartForm")
	ResponseDTO loadStartForm(@RequestParam String processdefinitionid);

	/**
	 * 保存开始表单数据并创建一个流程实例
	 *
	 * @param formid    最高版本的 表单定义id !!!
	 * @param userid    用户id
	 * @param tenantid  租户id
	 * @param applyid   企业端申请不需要保存表单, 但是需要有applyid
	 * @param formData  前端提交的表单数据
	 * @return
	 */
	@PostMapping(value = "flow/saveStartFormData")
	ResponseDTO saveStartFormData(
			@RequestParam String applyid,
			@RequestParam String userid,
			@RequestParam String formid,
			@RequestParam(required = false) String tenantid,
			@RequestBody Map<String, Object> formData
	);

	/**
	 * 创建子流程实例
	 * @param fainstid 父流程实例id
	 * @return
	 */
	@PostMapping(value = "flow/startSonProcess")
	ResponseDTO startSonProcess(
			@RequestParam String fainstid,
			@RequestParam String applyid,
			@RequestParam String userid,
			@RequestParam String formid,
			@RequestParam(required = false) String tenantid,
			@RequestBody Map<String, Object> formData
	);

	/**
	 * 完成业务
	 * @param handletext: 操作按钮的处理文字
	 * @param formid: 表单定义id
	 * @param taskid:  流程业务id
	 * @param userid
	 * @return
	 */
	@PostMapping("flow/taskExec")
	ResponseDTO taskExec(
			@RequestParam(required = false) String handletext,
			@RequestParam String formid,
			@RequestParam String taskid,
			@RequestParam String userid,
			@RequestBody ProcessNextExecutor executorData);

	/**
	 * 流程业务终止
	 *
	 * @param handletext:  操作按钮的处理文字
	 * @param notRequired: 补正查询不需要校验流程状态
	 * @param userid
	 * @param taskid
	 * @param deleteReason 终止原因
	 * @return
	 */
	@PostMapping("flow/terminationTask")
	ResponseDTO terminationTask(
			@RequestParam String userid,
			@RequestParam(required = false) String handletext,
			@RequestParam(required = false) Boolean notRequired,
			@RequestParam(required = false) String taskid,
			@RequestParam(required = false) String processinstanceid,
			@RequestParam String deleteReason,
			@RequestBody List<Map<String, Object>> serviceList);

	/**
	 * 查看流程进展图
	 *
	 * @param processinstanceid   (流程实例id, 对应act_ru_task表的PROC_INST_ID_字段)
	 * @throws Exception
	 */
	@PostMapping(value = "flow/viewProcessDiagram")
	Response viewProcessDiagram(@RequestParam String processinstanceid);

	/**
	 * 查看流程实例处理记录
	 *
	 * @param processinstanceid (流程实例id, 对应act_ru_task表的PROC_INST_ID_字段)
	 * @param pageinfo
	 * @return
	 */
	@PostMapping(value = "flow/queryTaskRecord")
	ResponseDTO queryTaskRecord(@RequestParam String processinstanceid,
			@RequestBody ExecutionPageInfo pageinfo);

	/**
	 * 业务委托由于某些原因不能处理该业务，可以把业务委派给用户B代理，当B处理完成之后再次回到用户A这里，在这个过程中A是业务的所有者，B是该业务的办理人.
	 * 业务属于A完成的
	 *
	 * @param taskid        业务id
	 * @param userid        委托人的userid
	 * @param entrustuserid 被委托人userid
	 * @return
	 */
	@PostMapping(value = "flow/delegate")
	ResponseDTO delegate(@RequestParam String taskid, @RequestParam String userid, @RequestParam String entrustuserid);

	/**
	 * 把业务转办给用户B完成, 业务属于B完成
	 *
	 * @param taskid         业务id
	 * @param userid 转办人
	 * @param turnuserid 被转办人
	 * @return
	 */
	@PostMapping(value = "flow/changeAssignee")
	ResponseDTO changeAssignee(@RequestParam String taskid, @RequestParam String userid, @RequestParam String turnuserid);

	/**
	 * 获取已处理的业务列表
	 *
	 * @param userid
	 * @param pageinfo
	 * @return
	 */
	@PostMapping("flow/endBusinessList")
	ResponseDTO endBusinessList(@RequestParam String userid, @Valid @RequestBody BusinessPageInfo pageinfo);

	/**
	 * 设置流程实例的停用和启用 (挂起/激活)
	 *
	 * @param processinstanceid 流程定义id
	 * @param suspensionState   1停用 2启用
	 * @return
	 */
	@PostMapping("flow/suspendOrActivateProcess")
	ResponseDTO suspendOrActivateProcess(@RequestParam String processinstanceid, @RequestParam String userid, @RequestParam Integer suspensionState,
	@RequestParam(required = false) String taskid, @RequestBody ProcessNextExecutor body);

	/**
	 * 流程业务驳回(默认驳回到上一节点, 传入stepid驳回到指定节点)
	 * @param userid
	 * @param backTaskVo 驳回实体类
	 * @return
	 */
	@PostMapping("flow/backProcess")
	ResponseDTO backProcess(@RequestParam String userid, @RequestBody BackTaskVo backTaskVo);

	/**
	 * 获取业务环节需要填写的表单数据
	 *
	 * @param taskid
	 * @param executionid 当前业务的执行id(对应act_ru_task表的EXECUTION_ID_字段)
	 * @return
	 */
	@PostMapping("flow/getTaskFormData")
	ResponseDTO getTaskFormData(@RequestParam String taskid, @RequestParam String executionid);

	/**
	 * 查询流程处理记录
	 * @param processinstanceid (流程实例id, 对应act_ru_execution表PROC_INST_ID_字段)
	 * @return
	 */
	@PostMapping("flow/queryComment")
	ResponseDTO queryComment(@RequestParam String processinstanceid);

	/**
	 * 查询流程最后一条处理记录
	 * @param processinstanceid (流程实例id, 对应act_ru_execution表PROC_INST_ID_字段)
	 * @return
	 */
	@PostMapping("flow/queryLastComment")
	ResponseDTO queryLastComment(@RequestParam String processinstanceid);

	/**
	 * 查询当前可驳回的流程节点
	 * @param stepid        当前环节id
	 * @param processinstanceid (流程实例id, 对应act_ru_execution表PROC_INST_ID_字段)
	 * @return
	 */
	@PostMapping("flow/getTaskdefList")
	ResponseDTO getTaskdefList(@RequestParam String stepid, @RequestParam String processinstanceid);

	/**
	 * 根据流程实例id查询进行中的执行实例
	 * @param processinstanceid (流程实例id, 对应act_ru_execution表PROC_INST_ID_字段)
	 * @return
	 */
	@PostMapping("flow/getProcessExecut")
	ResponseDTO getProcessExecut(@RequestParam String processinstanceid);

	/**
	 * 根据流程实例id查询已完成的执行实例
	 * @param processinstanceid (流程实例id, 对应act_ru_execution表PROC_INST_ID_字段)
	 * @return
	 */
	@PostMapping("flow/getProcessEnd")
	ResponseDTO getProcessEnd(@RequestParam String processinstanceid);

	  /**
     * 读取流程表单数据
     * @param formid
     * @param processinstanceid (流程实例id, 对应act_ru_execution表PROC_INST_ID_字段)
     * @param state 1 进行中流程实例表单数据, 2. 已完结的流程实例表单数据
     * @return
     */
    @PostMapping("flow/loadProcessForm")
    ResponseDTO loadProcessForm(@RequestParam String formid, @RequestParam String processinstanceid, @RequestParam Integer state);

    /**
     * 查询流程实例的流程变量
     * @param processinstanceid (流程实例id, 对应act_ru_execution表PROC_INST_ID_字段)
     * @return
     */
    @PostMapping("flow/getProcessVariable")
    ResponseDTO getProcessVariable(@RequestParam String processinstanceid);

	/**
	 * 查询处理页面所需参数
	 * @param taskid
	 * @return
	 */
	@PostMapping("flow/getHandleParams")
	ResponseDTO getHandleParams(@RequestParam String taskid);

	/**
	 * 待处任务导出excel
	 */
	@PostMapping("/flow/output")
	feign.Response output(@RequestParam String userid, @RequestBody TaskPageInfo pageinfo);

	/**
	 * 申请查询导出excel
	 */
	@PostMapping("/flow/applyQyeryOutput")
	feign.Response output(@RequestBody Map<String, Object> body);

	/**
	 * 激活父流程, 返回taskid
	 * @param processinstanceid
	 * @return
	 */
	@PostMapping("/flow/activationProcess")
	ResponseDTO activationProcess(@RequestParam String processinstanceid);

	/**
	 * 挂起父流程
	 * @param processinstanceid
	 * @return
	 */
	@PostMapping("/flow/suspendProcess")
	ResponseDTO suspendProcess(@RequestParam String processinstanceid);

	/**
	 * 查询父流程是否挂起
	 * @param processinstanceid
	 * @return
	 */
	@PostMapping("/flow/isSuspended")
	ResponseDTO isSuspended(@RequestParam String processinstanceid);

	/**
	 * 查询子流程关联的父流程id
	 * @param processinstanceid
	 * @return
	 */
	@PostMapping("/flow/queryFainstid")
	ResponseDTO queryFainstid(@RequestParam String processinstanceid);

	/**
	 * 保存流程过程记录
	 * @param handletext 操作按钮文字
	 * @param processinstanceid 流程实例id
	 * @param userid 操作用户id
	 * @param operation 操作类型枚举
	 * @param remark 意见
	 * @return
	 */
	@PostMapping("/flow/saveProcessRecord")
	ResponseDTO saveProcessRecord(
			@RequestParam String handletext,
			@RequestParam String processinstanceid,
			@RequestParam (required = false) String userid,
			@RequestParam String operation,
			@RequestParam String remark);

	/**
	 * 终止父流程关联的子流程
	 * @param fainstid 父流程id
	 * @param processinstanceid 子流程id (会跳过当前子流程)
	 * @param userid 用户id
	 * @param remark 审批意见
	 */
	@PostMapping("/flow/terminationSonProcess")
	ResponseDTO terminationSonProcess(
			@RequestParam String fainstid,
			@RequestParam String processinstanceid,
			@RequestParam String userid,
			@RequestParam String remark);

	/**
	 * 保存流程变量
	 * @param processinstanceid
	 * @param variables
	 * @return
	 */
	@PostMapping("/flow/saveVariable")
	ResponseDTO saveVariable(@RequestParam String processinstanceid,  @RequestBody Map variables);

	/**
	 * 流程往下走一步
	 *
	 * @param taskid
	 * @param variables
	 * @return
	 */
	@PostMapping("/flow/processNext")
	ResponseDTO processNext(@RequestParam String fromid,
							@RequestParam String applyid,
							@RequestParam String taskid,
							@RequestParam(required = false)  String userid,
							@RequestBody Map variables);
	/**
	 * 查询流程当前所处任务的taskid
	 * @param processinstanceid
	 * @return
	 */
	@PostMapping("/flow/queryTaskidByInstid")
	ResponseDTO queryTaskidByInstid(@RequestParam String processinstanceid);

	/**
	 * 根据流程实例id查询单个流程任务
	 * @param processinstanceid
	 * @return
	 */
	@PostMapping("/flow/getTaskByInstid")
	ResponseDTO getTaskByInstid(@RequestParam String processinstanceid);

	/**
	 * 根据流程实例id查询预览页面taskid
	 *
	 * @param processinstanceid
	 * @return
	 */
	@PostMapping("/flow/getViewTaskByInstid")
	ResponseDTO getViewTaskByInstid(@RequestParam String processinstanceid);

	/**
	 * 根据父流程实例id查询子流程记录
	 *
	 * @param processinstanceid
	 * @return
	 */
	@PostMapping("/flow/querySonProcessByInstid")
	ResponseDTO querySonProcessByInstid(@RequestParam String processinstanceid);

	/**
	 * 根据父流程任务id清除当前的流程处理人
	 *
	 * @param taskId
	 * @return
	 */
	@PostMapping("/flow/cleanClaimTask")
	ResponseDTO cleanClaimTask(@RequestParam String taskId);
}
